package io.bdmc.modules.bss.controller;

import io.bdmc.common.utils.R;
import io.bdmc.modules.bss.entity.DeviceEquipmentroom;
import io.bdmc.modules.bss.entity.DeviceFibreMatch;
import io.bdmc.modules.bss.entity.FibreFibre;
import io.bdmc.modules.bss.model.CablePointVo;
import io.bdmc.modules.bss.service.*;
import io.bdmc.modules.sys.controller.AbstractController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.*;

// "首页数据", description = "/bss/home")
@RestController
@RequestMapping("/bss/home")
public class HomeController extends AbstractController {
    @Autowired
    IDeviceEquipmentroomService _eqpSvs;

    @Autowired
    IFibreCablePointService _pointSvs;

    @Autowired
    IFibreFibreService _fibreSvs;

    @Autowired
    IDeviceAlarmService _alarmSvs;

    @Autowired
    IDeviceDeviceService _deviceSvs;

    @GetMapping("/sum")
    // "首页地图上的数据", notes = "首页地图上的数据")
    public R list() {
        List<DeviceEquipmentroom> listEqp = _eqpSvs.listByDeptId(getDeptId().intValue());
        Map<String, Object> data = new HashMap<>();
        data.put("equipmentroom", listEqp);
        List<FibreFibre> fibresNewList = new ArrayList<>();
        List<FibreFibre> fibresList = _fibreSvs.listByDeptId(getDeptId().intValue());
        List<DeviceFibreMatch> dfmList = _alarmSvs.deviceLastLengthAlarm();
        for (FibreFibre fibreFibre : fibresList) {
            if (fibreFibre.getPoints() != null && fibreFibre.getPoints().size() > 0) {
                List<CablePointVo> cablePointsList = fibreFibre.getPoints();
                cablePointsList.sort(Comparator.comparing(CablePointVo::getOrigoLength));
                CablePointVo tempPoint = null;
                for(CablePointVo cablePointVo : cablePointsList) {
                    if (cablePointVo.getPointType().equals("5")) {
                        if (tempPoint != null) {
                            final BigDecimal subtract = cablePointVo.getOrigoLength().subtract(tempPoint.getOrigoLength());
                            cablePointVo.setRoomLength(subtract);
                            cablePointVo.setLastRoomName(tempPoint.getPointName());
                        }
                        tempPoint = cablePointVo;
                    }
                }
                fibresNewList.add(fibreFibre);
            }
        }
        data.put("fibreList", fibresNewList);
        data.put("alarmPointList", dfmList);
        data.put("deviceFibreAlarmCount", _deviceSvs.deviceFibreAlarmCount(getDeptId()));
        return R.ok().putData(data);
    }

    @GetMapping("/count")
    // "首页统计数据", notes = "首页统计数据")
    public R count() {
        Map<String, Object> data = new HashMap<>();
        data.put("deviceFibreAlarmCount", _deviceSvs.deviceFibreAlarmCount(getDeptId()));
        data.put("alarmLevelSum", _alarmSvs.alarmTypeSum(getDeptId()));
        data.put("fibreLengthSum", _fibreSvs.fibreLengthSum(getDeptId()));
        data.put("fibreAlarmCount", _alarmSvs.alarmFibreCount(getDeptId()));
        data.put("deviceTypeSum", _deviceSvs.deviceTypeSum(getDeptId()));
        return R.ok().putData(data);
    }

    @GetMapping("/tp")
    // "拓扑图数据", notes = "拓扑图数据")
    public R tp() {
        return R.ok().putData(_pointSvs.tpList());
    }
}
